<?php
namespace Single;

use Phalcon\Db\Adapter\Pdo\Mysql as Database;
use Phalcon\Di;
use Phalcon\Http\Request;
use Phalcon\Http\Response;
use Phalcon\Loader;
use Phalcon\Mvc\Application as BaseApplication;
use Phalcon\Mvc\Dispatcher;
use Phalcon\Mvc\Model\Manager as ModelsManager;
use Phalcon\Mvc\Model\MetaData\Memory as MemoryMetaData;
use Phalcon\Mvc\Router;
use Phalcon\Mvc\View;

/**
 * Class Application
 */
class Application extends BaseApplication
{

    /**
     * This methods registers the services to be used by the application
     */
    protected function registerServices()
    {
        $di = new DI();

        // Registering a router
        $di->set('router', function () {

            return new Router();
        });

        // Registering a dispatcher
        $di->set('dispatcher', function () {
            $dispatcher = new Dispatcher();
            $dispatcher->setDefaultNamespace('Single\Controllers\\');

            return $dispatcher;
        });

        // Registering a Http\Response
        $di->set('response', function () {
            return new Response();
        });

        // Registering a Http\Request
        $di->set('request', function () {
            return new Request();
        });

        // Registering the view component
        $di->set('view', function () {
            $view = new View();
            $view->setViewsDir('../apps/Views/');

            return $view;
        });

        $di->set('db', function () {
            return new Database(
                [
                    "host" => "localhost",
                    "username" => "root",
                    "password" => "",
                    "dbname" => "invo"
                ]
            );
        });

        // Registering the Models-Metadata
        $di->set('modelsMetadata', function () {
            return new MemoryMetaData();
        });

        // Registering the Models Manager
        $di->set('modelsManager', function () {
            return new ModelsManager();
        });

        $this->setDI($di);
    }

    public function main()
    {
        $this->registerServices();

        $response = $this->handle();

        $response->send();
    }
}